const { ccclass, property } = cc._decorator;

import o from "./PropAniCtrl"
import i from "./MatchItemPrefabCtrl"
import r from "./MatchGameCenterCtrl"
import n from "./MatchGameViewCtrl"
import l from "./MatchGameUtilsDataMgr"
import s from "./EventMgr"
import c from "./MatchGameEventType"
import u from "./EngineUtil"
import p from "./MathUtil"
import d from "./TimeUtils"
import h from "./frameworkManager"
import f from "./MatchGameData"
import g from "./MatchGameConfig"
import y from "./FlyPropellerCtrl"
import m from "./ObstacleCtrl"
import _ from "./MatchGameAudioMgr"
import v from "./MatchGameDataMgr"
import b from "./MatchGameLogicDataCtrl"
import C from "./SquareStatusMgr"
import MathUtils from "./MathUtil";

@ccclass
class MatchGameEffectCtrl {

    static _instance;
    static _getInstance(): MatchGameEffectCtrl {
        this._instance || (MatchGameEffectCtrl._instance = new MatchGameEffectCtrl());
        return MatchGameEffectCtrl._instance;
    };
    public clearRowColEffect(e, t, a, i, n, l, s, c) {
        void 0 === s && (s = false);
        void 0 === c && (c = false);
        if (!s) if (a && a > 0) v.setGlobalScheduler(function () {
            for (var e = -1; e <= 1; e++) {
                var t = r._instance.displayMapInfo;
                t[i + e] && o.playOnePropUseBoomAni(g.PropType.RocketVertial, {
                    row: i + e,
                    col: n
                }, null);
                for (var a = false, l = 0; l < t[i].length; l++) t[i][l] && t[i][l] == g.DisplayMapType.Normal && (a = true);
                a && o.playOnePropUseBoomAni(g.PropType.RocketHorzontall, {
                    row: i,
                    col: n + e
                }, null);
            }
            _.playRocketAndTntAudio();
        }, .6); else {
            if (-1 != e) {
                _.playPropSingleExplodeAni(g.PropType.RocketHorzontall);
                o.playOnePropUseBoomAni(g.PropType.RocketVertial, {
                    row: i,
                    col: n
                }, null);
            }
            if (-1 != t) {
                _.playPropSingleExplodeAni(g.PropType.RocketHorzontall);
                o.playOnePropUseBoomAni(g.PropType.RocketHorzontall, {
                    row: i,
                    col: n
                }, null);
            }
        }
    };
    public clearDelateItems(e) {
        var t = [];
        for (var a = 0; a < e.length; a++) {
            if (a === 0) {
                t[0] = [];
                t[0].push(e[0]);
            } else if (m.isSameMulObstacle(e[a].item, e[a - 1].item)) {
                t[t.length - 1].push(e[a]);
            } else {
                t[t.length] = [];
                t[t.length - 1].push(e[a]);
            }
        }

        var o = 0;
        if (t.length) {
            var i = [];

            var r =  (e) => {
                if (t[e].length) {
                    var a, r = [], n = t[e][0].delyTime, l = t[e][0].count;

                    for (var s = 0; s < t[e].length; s++) {
                        if (t[e][s].delyTime > n) {
                            n = t[e][s].delyTime;
                            l = t[e][s].count;
                        }
                    }

                    a = 0.8 * (n + l);

                    for (var c = 0; c < t[e].length; c++) {
                        var u = t[e][c].item;
                        r.push(u);
                    }

                    h.log("横竖消除时间---2", a);
                    i.push(r);
                    o = Math.max(o, a);

                    v.setGlobalScheduler(function () {
                        v.clearItems2(r, false, true);
                    }, a);
                }
            };

            for (var n = 0; n < t.length; n++) {
                r(n);
            }

            h.log("totalClearArr==", i);

            v.setGlobalScheduler(function () {
                for (var e = 0; e < i.length; e++) {
                    var t = i[e];
                    if (t) {
                        var a = [];

                        for (var o = 0; o < t.length; o++) {
                            if (t[o] && t[o].node) {
                                var r = t[o];
                                var n = r._row;
                                var l = r._col;
                                var u = v.getSquareItemByPos(n, l);

                                if (u && v.isSquareInScene(u)) {
                                    a.push(u);
                                }
                            }
                        }

                        a.forEach( (e) => {
                            if (e && e.node) {
                                var t = e._row;
                                var a = e._col;

                                if (e.squareType == g.SquareType.MatchItem) {
                                    v.destoryItem(e);
                                }
                            }
                        });
                    }
                }

                s.trigger(c.ADD_ALL_FILL, {
                    callTime: d.getTimestamp(),
                    tTime: 0
                });
            }, o);
        } else {
            v.setGlobalScheduler(function () {
                if (v.checkCanFill()) {
                    s.trigger(c.ADD_ALL_FILL, {
                        callTime: d.getTimestamp(),
                        tTime: 0
                    });
                }
            }, 0.5);
        }

        return o;
    };

    public createPropeller(e, t, a, o) {
        _.playPropellerTakeOffAudio(a);
        var i = y.createFlyPropellerItem({
            i: e,
            j: t
        }, a), r = l.getPos(e, t);
        this.movePropeller(i, r, a, o);
    };
    public movePropeller(e, t, a, i) {
        var r = this, n = 90;
        1 == i ? n = 150 : 2 == i ? n = 30 : 3 == i && (n = 90);
        var y = MathUtils.getPosOnCircle(cc.v2(t), 40, n);
        0 == i && (y = cc.v2(t));
        _.playPropellerFlyAudio(a);
        var m, C = b.getOnePropllerDownPos(a), T = C.row, M = C.col, P = l.getPos(T, M), S = MathUtils.CalculationMidPoint(t, P, u.getRandomNumber(2, 8) / 10), w = MathUtils.getTwoPointsRadian1(cc.v2(t), cc.v2(P)), A = MathUtils.getPosOnCircle(cc.v2(S), u.getRandomNumber(50, 100), Math.random() > .5 ? w + 90 : w - 90);
        v.getSquareByRowCol(T, M);
        cc.tween(e.node).to(.3, {
            scale: 1.3,
            position: y
        }).bezierTo(1.3, cc.v2(y), cc.v2(A), P).call(function () {
            var t = f.getAllTarget(true);
            if (!b.getPropllerCanDown(T, M, a) && t && t.length) r.movePropeller(e, cc.v2(e.node.x, e.node.y), a, i); else {
                _.playPropellerHitAudio();
                var n = .1;
                switch (a) {
                    case g.PropType.RocketHorzontall:
                        n = b.clearRowCol(-1, C.col, 0, C.row, C.col, null, false, false);
                        break;

                    case g.PropType.RocketVertial:
                        n = b.clearRowCol(C.row, -1, 0, C.row, C.col, null, false, false);
                        break;

                    case g.PropType.Tnt:
                        n = b.clearGrid({
                            row: C.row,
                            col: C.col
                        }, 4, null, false);
                        break;

                    case -1:
                    case g.PropType.Propeller:
                        if (m = v.setReadyDestoryByPos(T, M)) {
                            v.clearItems2([m], true);
                            s.trigger(c.ADD_ALL_FILL, {
                                callTime: d.getTimestamp(),
                                tTime: 0
                            });
                        }
                }
                v.setGlobalScheduler(function () {
                    h.log("小飞机降落完成了喽~~~");
                    o.playPropellerDorpAni({
                        row: T,
                        col: M
                    }, null);
                    b.onFlyItemRecover();
                    s.trigger(c.ADD_ALL_FILL, {
                        callTime: d.getTimestamp(),
                        tTime: 0
                    });
                }, n);
                e.destorySelf1();
            }
        }).start();
    };
    public clearGridEffectSingle(e) {
        _.playPropSingleExplodeAni(g.PropType.Tnt);
        o.playOnePropUseBoomAni(g.PropType.Tnt, e, function () { });
    };
    public clearGridEffectMultiple(e) {
        _.playTntsUseAudio();
        _.playTntsBoomAudio();
        o.playDoubleTntAni(e, function () { });
    };
    public clearLightBallEffect(e, t, a, i) {
        var l = this, u = -1;
        n.instance.blockEliminate(true);
        var p = false;
        if (e && e == g.SquareType.PropItem) {
            p = true;
            u = v.getMaxItem(true);
        } else {
            p = false;
            u = t;
        }
        if (p && t == g.PropType.LightingBall) {
            r._instance.ui.LightingBallPlayingBg.active = true;
            _.playLightBallsUseAudio();
            _.playLightBallsBoomAudio();
            o.playLightingsBallAni({
                row: a.i,
                col: a.j
            }, function () { });
            v.scheduleOnce(function () {
                r._instance.ui.LightingBallPlayingBg.active = false;
                // v.clearAllSquare(a.i, a.j);
                v.clearAllSquare();
                n.instance.blockEliminate(false);
                b.clearUsedProp(a.i, a.j, true);
                i && b.clearUsedProp(i.i, i.j, false);
                s.trigger(c.ADD_ALL_FILL, {
                    callTime: d.getTimestamp(),
                    tTime: 0
                });
            }, 2);
            return 2;
        }
        i && b.clearUsedProp(i.i, i.j, false);
        if (!p) {
            t = v.getMaxItem(true);
            _.playLightBallSingleUseAudio();
        }
        var f = {
            row: a.i,
            col: a.j
        }, y = o.playLightBallUseAni(f);
        v.updateLightBallCount(1);
        for (var m = [], T = 0; T < v._map_row; T++) for (var M = 0; M < v._map_col; M++) {
            var P = v.getDestoryItem({
                row: T,
                col: M
            });
            if (P && P.squareType == g.SquareType.MatchItem && P.itemType == u && !v.isHaveObstacleInHigherLayout(P)) {
                C.readyDestorySquare(P);
                m.push(P);
            }
        }
        var S = m.length - 1, w = 0, A = .11 * m.length + 1.5;
        h.log("光球准备销毁的个数", m.length);
        if (0 == m.length) {
            if (p) {
                v.scheduleOnce(function () {
                    var e = t;
                    t != g.PropType.RocketVertial && t != g.PropType.RocketHorzontall || (e = Math.random() > .5 ? g.PropType.RocketVertial : g.PropType.RocketHorzontall);
                    b.clearUsedProp(a.i, a.j, true);
                    v.createPropItem(a.i, a.j, e);
                }, .5);
                v.scheduleOnce(function () {
                    v.useProp({
                        i: a.i,
                        j: a.j
                    });
                    o.recoverLightBallUseAni(y.node);
                    v.updateLightBallCount(-1);
                }, 1);
            } else v.scheduleOnce(function () {
                b.clearUsedProp(a.i, a.j, true);
                o.recoverLightBallUseAni(y.node);
                v.updateLightBallCount(-1);
                b.clearLightCross(a.i, a.j);
            }, 1);
            return 1.2;
        }
        v.schedule(function () {
            m[w] = l.playLightingAni(f, m[w], u, e, t);
            if (w == S) {
                for (var i = 0; i < v._map_row; i++) for (var r = 0; r < v._map_col; r++) if ((h = v.getDestoryItem({
                    row: i,
                    col: r
                })) && h.squareType == g.SquareType.MatchItem && h.itemType == u && C.getSquareNormalStatus(h) && !v.isHaveObstacleInHigherLayout(h) && -1 == m.indexOf(h)) {
                    h = l.playLightingAni(f, h, u, e, t);
                    m.push(h);
                    C.readyDestorySquare(h);
                    S++;
                }
                if (v.getPropItem(a.i, a.j) && C.getSquareReadyDestoryStatus(v.middleSquareGrid[a.i][a.j])) {
                    var h = l.playLightingAni(f, v.middleSquareGrid[a.i][a.j], u, e, t);
                    C.readyDestorySquare(h);
                    m.push(h);
                }
                v.scheduleOnce(function () {
                    if (p) {
                        var a = .1;
                        e == g.SquareType.PropItem && t == g.PropType.Tnt && (a = 0);
                        var i = m.length, r = 0;
                        v.schedule(function () {
                            var e = m[r];
                            if (e && e.node && C.getSquareReadyDestoryStatus(e) && e.squareType == g.SquareType.PropItem) {
                                var t = m[r], a = t._row, o = t._col;
                                v.useProp({
                                    i: a,
                                    j: o
                                });
                            }
                            r++;
                        }, a, i - 1, 0);
                    } else {
                        _.stopLightBallSingleUseAudio();
                        v.matchDestoryItems(m, null);
                        s.trigger(c.ADD_ALL_FILL, {
                            callTime: d.getTimestamp(),
                            tTime: 0
                        });
                    }
                    o.recoverLightBallUseAni(y.node);
                    v.updateLightBallCount(-1);
                    _.playLightBallBoomAudio();
                    n.instance.blockEliminate(false);
                }, 1);
            }
            w++;
        }, .1, S, .11);
        return A;
    };
    public playLightingAni(e, t, a, r, n) {
        h.log("playLightingAni---");
        if (t.node.isValid) {
            var l = t._row, s = t._col;
            _.playLightBallRayAudio();
            if (r == g.SquareType.PropItem) {
                t && !C.getSquareDestoryStatus(t) && v.destoryItem(t);
                var c = n;
                n != g.PropType.RocketVertial && n != g.PropType.RocketHorzontall || (c = Math.random() > .5 ? g.PropType.RocketVertial : g.PropType.RocketHorzontall);
                var u = v.createPropItem(l, s, c);
                C.readyDestorySquare(u);
                u.createdByLightBall = true;
                u.playShakeAni();
                o.playLightingAni(e, {
                    row: t._row,
                    col: t._col
                }, function () { });
                return u;
            }
            r == g.SquareType.MatchItem && o.playLightingAni(e, {
                row: t._row,
                col: t._col
            }, function () {
                t && t.squareType == g.SquareType.MatchItem && t.getComponent(i) && t.playLightingAni();
            });
            return t;
        }
    }
}

export default MatchGameEffectCtrl._getInstance();
